package cn.sfa.common.jms;

import java.util.HashMap;
import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.jms.support.converter.MessageConverter;
/**
 * 
* 类名称：ResourceMessageConverter   
* 类描述： 消息转换器
* 创建人：lenovo   
* 创建时间：2012-2-9 下午6:07:37   
* 修改备注：   
* @version v1.0
 */
public class ResourceMessageConverter implements MessageConverter {

	/*
	 * (non-Javadoc)
	 *
	 * @see org.springframework.jms.support.converter.MessageConverter#toMessage(java.lang.Object,
	 *      javax.jms.Session)
	 */
	public Message toMessage(Object obj, Session session) throws JMSException {
		// check Type
		//if (obj instanceof Book) {
			ActiveMQObjectMessage objMsg = (ActiveMQObjectMessage) session
					.createObjectMessage();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("obj", obj);
			objMsg.setObjectProperty("Map", map);
			return objMsg;
			/*} else {
			throw new JMSException("Object:[" + obj + "] is not Book");
		}*/

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.springframework.jms.support.converter.MessageConverter#fromMessage(javax.jms.Message)
	 */
	public Object fromMessage(Message msg) throws JMSException {
		if (msg instanceof ObjectMessage) {
			return ((Map) ((ObjectMessage) msg).getObjectProperty("Map"))
					.get("obj");
		} else {
			throw new JMSException("Msg:[" + msg + "] is not Map");
		}
	}

}
